package main

import (
	"net/http"

	"github.com/julienschmidt/httprouter"
)

type middleWareHanler struct {
	r *httprouter.Router
}

func NewMiddleWareHandler(r *httprouter.Router) http.Handler {
	m := middleWareHanler{}
	m.r = r
	return m
}

func (m middleWareHanler) ServeHTTP(w http.ResponseWriter, r *http.Request) {
	// 检查session
	validateUserSession(r)
	m.r.ServeHTTP(w, r)
}

func RegisterHandlers() *httprouter.Router {
	router := httprouter.New()

	router.POST("/user", CreateUser)
	router.POST("/user/:user_name", Login)

	// router.GET("/", Index)
	// router.GET("/user/:id", GetUser)
	// router.POST("/user", AddUser)
	// router.PUT("/user/:id", UpdateUser)
	// router.DELETE("/user/:id", DeleteUser)

	return router
}

func main() {
	r := RegisterHandlers()
	mh := NewMiddleWareHandler(r)
	http.ListenAndServe(":8000", mh)
}

// handler->validation{1.request, 2.user}->business logic->response.
// 1. data model
// 2. error handling.
